<template>
  <el-card shadow="never">
    <div class="system-user-search mb15">
      <el-input
        v-model="state.tableData.param.search"
        size="default"
        placeholder="请输入客户姓名/公司名称"
        style="max-width: 180px"
      >
      </el-input>
      <el-select placeholder="根据姓名搜索" style="max-width: 150px" class="ml10" clearable>
        <el-option label="根据姓名搜索" value="0" />
        <el-option label="根据公司搜索" value="1" />
      </el-select>
      <el-button size="default" type="primary" class="ml10"> <i-ep-search /> 查询 </el-button>
      <el-button size="default" type="success" class="ml10" @click="onOpenAddRole('add')">
        <i-ep-plus /> 新增客户
      </el-button>
    </div>
    <el-scrollbar max-height="600px">
      <template #header>
        <div class="table-header">
          <div>
            <el-button size="default" type="success" class="ml10" @click="onOpenAddRole('add')">
              <i-ep-plus /> 新增角色
            </el-button>
            <el-button type="danger" @click="handleDelete()"><i-ep-delete />删除</el-button>
          </div>
          <div>
            <el-button @click="openDialog('user-import')"><i-ep-top />导入 </el-button>
            <el-button class="ml-5" @click="handleExport"><i-ep-download />导出</el-button>
          </div>
        </div>
      </template>
      <el-table
        :data="state.tableData.data"
        v-loading="state.tableData.loading"
        style="width: 100%"
      >
        <el-table-column type="index" label="序号" width="60" />
        <el-table-column prop="name" label="客户姓名" show-overflow-tooltip></el-table-column>
        <el-table-column prop="type" label="客户类型" show-overflow-tooltip>
          <template #default="scope">
            <el-tag type="success" v-if="scope.row.type === 1">供货商</el-tag>
            <el-tag type="info" v-else>销售商</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="company" label="公司名称" show-overflow-tooltip></el-table-column>
        <el-table-column prop="telephone" label="电话" show-overflow-tooltip></el-table-column>
        <el-table-column prop="address" label="地址" show-overflow-tooltip></el-table-column>
        <el-table-column prop="createTime" label="创建时间" show-overflow-tooltip></el-table-column>
        <el-table-column label="操作" align="center" fixed="right" width="150">
          <template #default="scope">
            <el-button size="small" text type="primary" @click="onOpenEditDic('edit', scope.row)">
              修改
            </el-button>
            <el-button size="small" text type="primary" @click="onRowDel(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="onHandleSizeChange"
        @current-change="onHandleCurrentChange"
        class="mt15"
        :pager-count="5"
        :page-sizes="[10, 20, 30]"
        v-model:current-page="state.tableData.param.pageNum"
        background
        v-model:page-size="state.tableData.param.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="state.tableData.total"
      >
      </el-pagination>
    </el-scrollbar>
  </el-card>
  <RoleDialog ref="roleDialogRef" @refresh="getTableData()" />
</template>

<script setup>
import { defineAsyncComponent, reactive, onMounted, ref } from 'vue'
// 引入组件
const RoleDialog = defineAsyncComponent(() => import('@/views/manage/customCard/dialog.vue'))

// 定义变量内容
const roleDialogRef = ref()
const state = reactive({
  tableData: {
    data: [],
    total: 0,
    loading: false,
    param: {
      search: '',
      pageNum: 1,
      pageSize: 10
    }
  }
})
// 初始化表格数据
const data = [
  {
    id: 1,
    name: '张三',
    type: 1,
    company: '省进出口总公司',
    telephone: '184556xxxx',
    address: '省XX区哪里哪里',
    createTime: '2024-1-20'
  },
  {
    id: 2,
    name: '李四',
    type: 2,
    company: '省进出口总公司',
    telephone: '184556xxxx',
    address: '省XX区哪里哪里',
    createTime: '2024-1-20'
  },
  {
    id: 3,
    name: '王老总',
    type: 1,
    company: '省进出口总公司',
    telephone: '184556xxxx',
    address: '省XX区哪里哪里',
    createTime: '2024-1-20'
  },
  {
    id: 4,
    name: '张总',
    type: 2,
    company: '省进出口总公司',
    telephone: '184556xxxx',
    address: '省XX区哪里哪里',
    createTime: '2024-1-20'
  },
  {
    id: 5,
    name: '伍拾伊',
    type: 1,
    company: '省进出口总公司',
    telephone: '184556xxxx',
    address: '省XX区哪里哪里',
    createTime: '2024-1-20'
  },
  {
    id: 6,
    name: '小李四',
    type: 1,
    company: '省进出口总公司',
    telephone: '184556xxxx',
    address: '省XX区哪里哪里',
    createTime: '2024-1-20'
  },
  {
    id: 7,
    name: '陆五沦',
    type: 1,
    company: '省进出口总公司',
    telephone: '184556xxxx',
    address: '省XX区哪里哪里',
    createTime: '2024-1-20'
  },
  {
    id: 8,
    name: '李三光',
    type: 2,
    company: '省进出口总公司',
    telephone: '184556xxxx',
    address: '省XX区哪里哪里',
    createTime: '2024-1-20'
  },
  {
    id: 9,
    name: '亿八',
    type: 2,
    company: '省进出口总公司',
    telephone: '184556xxxx',
    address: '省XX区哪里哪里',
    createTime: '2024-1-20'
  },
  {
    id: 10,
    name: '林石',
    type: 2,
    company: '省进出口总公司',
    telephone: '184556xxxx',
    address: '省XX区哪里哪里',
    createTime: '2024-1-20'
  }
]

const getTableData = () => {
  state.tableData.loading = true
  state.tableData.data = data
  state.tableData.total = state.tableData.data.length
  setTimeout(() => {
    state.tableData.loading = false
  }, 500)
}
// 打开新增角色弹窗
const onOpenAddRole = (type) => {
  roleDialogRef.value.openDialog(type)
}
// 打开修改角色弹窗
const onOpenEditRole = (type, row) => {
  roleDialogRef.value.openDialog(type, row)
}
// 删除角色
const onRowDel = (row) => {
  ElMessageBox.confirm(`此操作将永久删除角色名称：“${row.roleName}”，是否继续?`, '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(() => {
      getTableData()
      ElMessage.success('删除成功')
    })
    .catch(() => {})
}
// 分页改变
const onHandleSizeChange = (val) => {
  state.tableData.param.pageSize = val
  getTableData()
}
// 分页改变
const onHandleCurrentChange = (val) => {
  state.tableData.param.pageNum = val
  getTableData()
}
// 页面加载时
onMounted(() => {
  getTableData()
})
</script>
<style lang="scss" scoped>
.form-wrapper {
  margin: 0 1.8rem;
  .el-form {
    display: flex;
    align-items: center;
    height: 5rem;
    .el-form-item {
      margin-bottom: 0;
    }
  }
}
</style>
